package com.lab;

/*
 * @(#)Student.java    Dec 4, 2009
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

/**
 * 当类实现Comparable时, 实例化集合可以不用制定比较器, 否则抛出java.lang.ClassCastException.
 * 
 * <p>注意 <b>Comparable</b>和 <b>Comparator</b>的区别</p>.
 *
 * @author nickevin@gmail.com
 * @version Dec 4, 2009 4:13:19 PM
 */

public class Student implements Comparable<Student> {

    private String firstName;
    private String lastName;
    private String birthday;
    private Integer age;

    public Student() {}

    public Student(String firstName, String lastName, String birthday,
            Integer age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = birthday;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (birthday == null ? 0 : birthday.hashCode());
        result = prime * result
                + (firstName == null ? 0 : firstName.hashCode());
        result = prime * result + (lastName == null ? 0 : lastName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Student other = (Student) obj;
        if (birthday == null) {
            if (other.birthday != null) {
                return false;
            }
        } else if (!birthday.equals(other.birthday)) {
            return false;
        }
        if (firstName == null) {
            if (other.firstName != null) {
                return false;
            }
        } else if (!firstName.equals(other.firstName)) {
            return false;
        }
        if (lastName == null) {
            if (other.lastName != null) {
                return false;
            }
        } else if (!lastName.equals(other.lastName)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "My name is " + firstName + " " + lastName
                + " , my birthday is " + birthday + " and i am " + age
                + " years old!" + "\r";
    }

    public int compareTo(Student b) {
        if (this == null || b == null) {

            return 0;
        }

        return getLastName().compareTo(b.getLastName());
    }
}
